package com.walter;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.HBox;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

/**
 * 时间选择器
 */
public class DateTimePicker extends HBox {
    private DatePicker datePicker;
    protected ComboBox<String> hourComboBox;
    protected ComboBox<String> minuteComboBox;
    protected ComboBox<String> secondComboBox;

    protected String hour;
    protected String minute;
    protected String second;

    /**
     * 默认展示当前日期和时间
     */
    public DateTimePicker() {
        this(System.currentTimeMillis());
    }

    /**
     * 初始化指定时间
     *
     * @param time 时间毫秒值
     */
    public DateTimePicker(long time) {
        this(DateUtil.toLocalDateTime(new Date(time)));
    }

    /**
     * 初始化指定时间
     *
     * @param date 指定时间
     */
    public DateTimePicker(LocalDateTime date) {
        this(date.toLocalDate(), date.getHour(), date.getMinute(), date.getSecond());
    }

    /**
     * 初始化指定时间
     *
     * @param date   日期
     * @param hour   小时
     * @param minute 分钟
     * @param second 秒
     */
    public DateTimePicker(LocalDate date, int hour, int minute, int second) {
        this.hour = getTimeStr(hour);
        this.minute = getTimeStr(minute);
        this.second = getTimeStr(second);

        datePicker = new DatePicker(date);

        hourComboBox = getTime(23);
        hourComboBox.getSelectionModel().select(this.hour);
        addHourListener(hourComboBox);

        minuteComboBox = getTime(59);
        minuteComboBox.getSelectionModel().select(this.minute);
        addMinuteListener(minuteComboBox);

        secondComboBox = getTime(59);
        secondComboBox.getSelectionModel().select(this.second);
        addSecondListener(secondComboBox);

        this.getChildren().addAll(datePicker, new HBox(hourComboBox, minuteComboBox, secondComboBox));
        this.setSpacing(10);
    }

    /**
     * 将时间组件内容格式化成指定的的日期格式
     *
     * @param format 日期格式
     * @return 指定日期格式的字符串
     */
    public String format(String format) {
        LocalTime localTime = LocalTime.of(getTimeInteger(this.hour), getTimeInteger(this.minute), getTimeInteger(this.second));
        LocalDateTime localDateTime = LocalDateTime.of(datePicker.getValue(), localTime);
        return DateUtil.format(localDateTime, format);
    }

    protected DateTimePicker self() {
        return this;
    }

    private int getTimeInteger(String time) {
        return NumberUtil.parseInt(time);
    }

    private static String getTimeStr(String time) {
        if (time.length() < 2) {
            return "0" + time;
        }
        return time;
    }

    private static String getTimeStr(int time) {
        return getTimeStr(String.valueOf(time));
    }

    private void addHourListener(ComboBox<String> comboBox) {
        comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> this.hour = newValue);
    }

    private void addMinuteListener(ComboBox<String> comboBox) {
        comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> this.minute = newValue);
    }

    private void addSecondListener(ComboBox<String> comboBox) {
        comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> this.second = newValue);
    }

    private ComboBox<String> getTime(int x) {
        ComboBox<String> time = new ComboBox<>();
        for (int i = 0; i <= x; i++) {
            time.getItems().add(getTimeStr(i));
        }
        return time;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public DatePicker getDatePicker() {
        return datePicker;
    }

    public void setDatePicker(DatePicker datePicker) {
        this.datePicker = datePicker;
    }

    public ComboBox<String> getHourComboBox() {
        return hourComboBox;
    }

    public void setHourComboBox(ComboBox<String> hourComboBox) {
        this.hourComboBox = hourComboBox;
    }

    public ComboBox<String> getMinuteComboBox() {
        return minuteComboBox;
    }

    public void setMinuteComboBox(ComboBox<String> minuteComboBox) {
        this.minuteComboBox = minuteComboBox;
    }

    public ComboBox<String> getSecondComboBox() {
        return secondComboBox;
    }

    public void setSecondComboBox(ComboBox<String> secondComboBox) {
        this.secondComboBox = secondComboBox;
    }
}
